import type { InternalAxiosRequestConfig } from 'axios'
import { db } from '@/storage'
import { tokenConfig, configSetting } from '@/config'
import router from '@/router'

export const handlerRequstConfig = (config: InternalAxiosRequestConfig) => {
  const { key, value } = tokenConfig.token
  const token = db.get('token')
  const tokenKeys: Record<string, string> = {}
  if (key) {
    tokenKeys[key] = value.split(' ')[0] + ' ' + token
  }
  let _header: any = {}
  if (token) {
    _header = {
      ..._header,
      ...tokenKeys
    }
  }

  // 查询是否超时
  const time = db.get('expires')
  if (new Date().getTime() - time > tokenConfig.token?.expires) {
    router.push({
      name: configSetting.LoginMainLayoutName
    })
  }
  return { ...config, headers: { ...config.headers, ..._header } }
}
